<template>
	<div class="container">
		<div class="JueSeGuanLi">
			<div class="JueSeList">
				<div style="display: flex;">
					<div class="js">角色</div>
					<div class="k"></div>
					<div class="zdyjs" @click="showCustomRoles">自定义角色</div>
				</div>
				<div class="jslist">
					<ul>
						<li v-for="(item, index) in items.jueSe" :key="index" @click="toggleHighlight(index)"
							:class="{ highlighted: highlightedIndex === index }">
							<span class="xtmr" v-if="index < 2">
								系统默认
							</span>
							<span>{{item.roleRemark}}</span>
						</li>
					</ul>
				</div>
			</div>
		</div>
		<div v-if="currentTab === 'rolePermissions'" class="QuanXian">
			<div class="JueSeQuanXian">
				<div class="JueSeQuanXian1">{{selectedRole}}</div>
				<div class="JueSeQuanXian2"></div>
				<div class="JueSeQuanXian3">
					<el-button v-if="highlightedIndex !== -1 && highlightedIndex >= 2"
						class="btuJueSeQuanXian1" @click="switchToUpdatePage">编辑</el-button>
					<el-button v-if="highlightedIndex !== -1 && highlightedIndex >= 2"
						class="btuJueSeQuanXian2">删除</el-button>
				</div>
			</div>
		</div>
		<div v-if="currentTab === 'customRoles'" class="ZiDingYiJueSe">
			<div class="TianJiaJueSe">
				<el-form style="flex: 1">
					<el-form-item label="角色名称" style="margin-left: 25px;margin-top: 30px;">
						<el-input v-model="JueSeName.roleName" placeholder="输入岗位/角色,管家,前台等" style="width: 300px;"/>
					</el-form-item>
				</el-form>
				<div class="btuTianJiaJueSe">
					<el-button class="btuTianJiaJueSe1" @click="addJueSe">保存</el-button>
					<el-button class="btuTianJiaJueSe2">取消</el-button>
				</div>
			</div>
		</div>
		<div v-if="currentTab === 'updateRole'" class="UpdateJueSe">
			<div class="UpdJueSe">
				<el-form style="flex: 1">
					<el-form-item label="角色名称" style="margin-left: 25px;margin-top: 30px;">
						<el-input v-model="selectedRole" placeholder="输入岗位/角色,管家,前台等" style="width: 300px;"/>
					</el-form-item>
				</el-form>
				<div class="btuUpdJueSe">
					<el-button class="btuUpdJueSe1" @click="updateJueSe">保存</el-button>
					<el-button class="btuUpdJueSe2">取消</el-button>
				</div>
			</div>
		</div>
	</div>
</template>

<script setup lang="ts">
	import { onMounted, ref,reactive } from 'vue';
	import $http from '../router/AxiosApi';
	import { ElMessage } from 'element-plus'
	const selectedRole = ref('')
	const highlightedIndex = ref(-1);
	const roleId=ref('')
	function toggleHighlight(index : number) {
		 if (index >= 0 && index < items.jueSe.length) {
		    highlightedIndex.value = index;
		    selectedRole.value = items.jueSe[index].roleRemark;
			roleId.value=items.jueSe[index].roleId
		    currentTab.value = 'rolePermissions';
		  } else {
		    console.error('Index out of range');
		  }
	}
	onMounted(() => {
		toggleHighlight(0); // 默认选中超级管理员
	});

	const currentTab = ref('rolePermissions');
	
	function showCustomRoles() {
		currentTab.value = 'customRoles';
	}
	function switchToUpdatePage(){
		currentTab.value = 'updateRole'
	}
	const updateJueSe=()=>{
		const form=reactive({
			roleId:'',
			roleRemark:''
		})
		form.roleId=roleId.value
		form.roleRemark=selectedRole.value
		console.log(form)
		$http.put("updateJueSe",form).then((res)=>{
			console.log(res.data)
			ElMessage({
				message: '角色修改成功',
				type: 'success',
				plain: true,
			})
			loadJue()
		})
	}
	const JueSeName=reactive({
		roleName:'',
		roleRemark:''
	})
	const addJueSe=()=>{
		JueSeName.roleRemark=JueSeName.roleName
		$http.post("addJueSe",JueSeName).then((res)=>{
			ElMessage({
				message: '角色添加成功',
				type: 'success',
				plain: true,
			})
			loadJue()
		})
	}
	const items = reactive({
		jueSe:[]
	})
	const loadJue=()=>{
		$http.post("findJueSe").then((res)=>{
			items.jueSe=res.data
			console.log(items.jueSe);
			
		})
	}
	onMounted(()=>{
		loadJue()
	})
</script>

<style scoped>
	.container {
		display: flex;
		width: 1465px;
	}

	.JueSeGuanLi {
		flex: 1;
	}

	.JueSeList {
		width: 350px;
		background-color: white;
		border-radius: 10px;
	}

	.js {
		width: 40px;
		height: 30px;
		display: flex;
		margin-left: 20px;
		margin-top: 15px;
		font-weight: bold;
	}

	.k {
		width: 205px;
		height: 30px;
		display: flex;
		margin-top: 15px;
	}

	.zdyjs {
		width: 90px;
		height: 30px;
		display: flex;
		margin-top: 15px;
		margin-right: 10px;
		cursor: pointer;
		color: #475487;
	}

	.jslist ul {
		list-style-type: none;
		padding: 0;
		padding-bottom: 10px;
	}

	.jslist li {
		padding: 5px 10px;
		height: 55px;
		width: 300px;
		margin-left: 15px;
		transition: background-color 0.3s;
		cursor: pointer;
		border-radius: 8px;
		line-height: 55px;
	}

	.jslist li:hover {
		background-color: #f0f0f0;
	}

	.xtmr {
		margin-right: 20px;
		color: rgb(0, 195, 109);
		padding: 4px 15px 4px 20px;
		background-color: rgb(229, 249, 240);
		font-weight: normal;
		border-radius: 5px;
		margin-left: -115px;
	}

	.highlighted {
		color: #00c36d;
		font-weight: bold;
	}

	.QuanXian {
		width: 1100px;
		border-radius: 10px;
		background-color: white;
	}

	.JueSeQuanXian {
		height: 100px;
		background-color: #f0f0f0;
		margin-left: 15px;
		margin-top: 20px;
		margin-right: 15px;
	}

	.JueSeQuanXian1 {
		margin-left: 15px;
		margin-top: 10px;
		width: 100px;
		height: 50px;
		font-size: 20px;
		font-weight: bold;
	}

	.JueSeQuanXian2 {
		margin-top: 10px;
		width: 850px;
		height: 50px;
	}

	.JueSeQuanXian3 {
		margin-top: 10px;
		margin-left: 50px;
		width: 200px;
		height: 50px;
	}

	.btuJueSeQuanXian1 {
		width: 80px;
		height: 30px;
		border: none;
		background-color: #00c36d;
		color: white;
	}

	.btuJueSeQuanXian2 {
		width: 80px;
		height: 30px;
		border: none;
		background-color: white;
		color: #ccc;
	}

	.QuanXianList {
		width: 1240px;
		border: 1px solid #ccc;
		margin-left: 20px;
		border-radius: 10px;
		margin-top: 5px;
		display: flex;
		margin-bottom: 10px;
	}

	.QuanXianList1 {
		margin-top: 20px;
		width: 400px;
		border-right: 1px solid #ccc;
		margin-left: 10px;
		margin-bottom: 10px;
	}

	.QuanXianList2 {
		width: 400px;
		border-right: 1px solid #ccc;
		margin-bottom: 10px;
		margin-top: 20px;
	}

	.QuanXianList3 {
		width: 400px;
		height: 300px;
	}

	.ZiDingYiJueSe {
		width: 1100px;
		border-radius: 10px;
		background-color: white;
	}

	.TianJiaJueSe {
		height: 100px;
		background-color: #f0f0f0;
		margin-top: 20px;
		margin-left: 15px;
		margin-right: 15px;
		display: flex;
	}

	.btuTianJiaJueSe {
		width: 200px;
		margin-top: 30px;
		margin-right: 30px;
	}
	.btuTianJiaJueSe1{
		width: 80px;
		height: 30px;
		border: none;
		background-color: #00c36d;
		color: white;
	}
	.btuTianJiaJueSe2{
		width: 80px;
		height: 30px;
		border: none;
		background-color: white;
	}
	.UpdateJueSe{
		width: 1100px;
		border-radius: 10px;
		background-color: white;
	}
	.UpdJueSe{
		height: 100px;
		background-color: #f0f0f0;
		margin-top: 15px;
		margin-left: 15px;
		display: flex;
	}
	.btuUpdJueSe{
		width: 200px;
		margin-top: 30px;
		margin-right: 30px;
	}
	.btuUpdJueSe1{
		width: 80px;
		height: 30px;
		border: none;
		background-color: #00c36d;
		color: white;
	}
	.btuUpdJueSe2{
		width: 80px;
		height: 30px;
		border: none;
		background-color: white;
	}
</style>